# test02_thread.py  shutil/os/subprocess
# data = subprocess.run("xxx", shell=True)
# data.args  data.returncode   $?   0  !0
# 调用 ping 命令
# - 编写 ping 函数   def ping():
# - 用于测试远程主机的联通性
# - ping 通显示：x.x.x.x:up
# - ping 不通显示：x.x.x.x:down
# 测试: 单线程/多线程方式
import subprocess, threading
def ping(host):
    data = subprocess.run(
        f"ping -c 2 {host} &> /dev/null",shell=True
    )
    if data.returncode != 0:
        print(f"{host}: down")
    else:
        print(f"{host}: up")
if __name__ == '__main__':
    ips = ["www.163.com","www.qq.com","www.baidu.com","www.163.com","www.qq.com","www.baidu.com","www.163.com","www.qq.com","www.baidu.com"]
    for ip in ips:  # 多线程
        t = threading.Thread(target=ping,args=(ip,))
        t.start()
    # 单线程
    # for ip in ips:
    #     ping(ip)
